/**
 * 客户价值评估维护
 * @author liuwei
 * @date 2019/11/18
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_head_where = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWBYENT',width:100,hide:true},
            {field: 'GWBY001',title:'评估编号',width:100,search:"default",searchOpts:{icon:true}},
            {field: 'GWBY002_NAME',title:'客户',width:100},
            {field: 'GWBY003',title:'总得分',width:80,sort:true},
            {field: 'GWBYDATE',title:'评审日期',width:100,sort:true,search:"default",searchOpts:{type:"date"}},
            {field: 'GWBYORIU_NAME',title:'创建人',width:100},
            {field: 'GWBYORIG_NAME',title:'所属组织',width:100},
            {field: 'GWBY004',title:'提交时间',width:130,sort:true},
            {field: 'GWBY005',title:'审核时间',width:130,sort:true},
            {field: 'GWBYACTI',title:'状态',width:100,filterDatas:app.datas_("gwbyacti")},


            {field: 'GWBY002',title:'客户',width:100,hide:true,search:"default",searchOpts:{icon:true}},
            {field: 'GWBYORIU',title:'创建人',width:100,hide:true,search:"default",searchOpts:{icon:true}},
            {field: 'GWBYORIG',title:'所属组织',width:100,hide:true},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai305setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        moduleName = "客户价值评估信息",
        gwai305SettingAction
    ;

    var Gwai300 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai305',
                _searchs: [{
                    id: 'gwai305', tableId: 'gwai305_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {
                        dom: 'gwai305_edit', tpl: 'gwai305_edit'
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        initHeadTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai305_table'),
                    url: g_ctxPath + 'crm/gwai305/getHeadList',
                    exportsUrl: g_ctxPath + 'crm/gwai305/getHeadList',
                    exportsName: '客户价值评估数据',
                    title: '客户价值评估信息表',
                    method: 'post',
                    id: 'gwai305_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai305setting-action').innerHTML).render({}, function (string) {
                    gwai305SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai305_table)', function (obj) {

                    layer.tips(gwai305SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':

                                        self._openTplFull({
                                            tag: 'gwai305_edit',
                                            tagData: {
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '客户价值评估信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });


                //监听行双击事件
                table.on('rowDouble(gwai305_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai305_edit',
                        tagData: {
                            detailReadonly:true,
                            obj: obj
                        },
                        index: 0,
                        openOpts: {
                            title: '客户价值评估信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai305_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai305_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '客户价值评估信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'submit' :
                            self.changeStatusBatch("submit");
                            break;
                        case 'cancelSubmit' :
                            self.changeStatusBatch("cancelSubmit");
                            break;
                        case 'check' : // 审核
                            self.changeStatusBatch("check");
                            break;
                        case 'revoke' : // 退回
                            self.changeStatusBatch("revoke");
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai305_table', where: table_head_where});
        },

        changeStatusBatch:function(action) {
            var checkStatus = table.checkStatus('gwai305_table');
            var flag = true;
            if(checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    let _temp = app.fields_('GWBY_T');
                    switch (action) {
                        case "submit" :  // 提交
                            if( v.GWBYACTI === "N" || v.GWBYACTI === 'R'){
                                v.GWBYACTI = "T"
                            }
                            else {
                                app.showWarning("未审核或退回状态，才能进行提交操作");
                                flag = false;
                                return;
                            }
                            break;
                        case "cancelSubmit" : // 取消提交
                            if(v.GWBYACTI === "T"){
                                v.GWBYACTI = "N"
                            }
                            else {
                                app.showWarning("已提交的评估项才能取消提交");
                                flag = false;
                                return;
                            }
                            break;
                        case "check" : // 审核
                            if(v.GWBYACTI === "T"){
                                v.GWBYACTI = "Y"
                            }
                            else {
                                app.showWarning("已提交的评估项才能审核");
                                flag = false;
                                return;
                            }
                            break;
                        case "revoke" : // 退回
                            if(v.GWBYACTI === "T"){
                                v.GWBYACTI = "R"
                            }
                            else {
                                app.showWarning("已提交的评估项才能退回");
                                flag = false;
                                return;
                            }
                            break;
                    }
                    _xdata.push(v);
                });
                if(!flag){
                    return;
                }else {
                    app.ajax({
                        xurl: 'crm/gwai305/changeStatus',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName +'状态修改成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        delData: function (obj) {
            if(obj.data.GWBYACTI !== "N"){
                app.showWarning("只有未审核的资料可以删除");
                return;
            }
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai305/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '客户价值评估信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('gwai305_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var canDelete = true;
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwai305');
                    if(v.GWBYACTI !== "N"){
                        canDelete = false;
                        return;
                    }
                    _temp.GWBY001 = v.GWBY001;
                    _temp.GWBYENT = v.GWBYENT;
                    _xdata.push(_temp);
                });
                if(!canDelete){
                    app.showWarning("只有未审核的资料可以删除");
                    return;
                }
                app.ajax({
                    xurl: 'crm/gwai305/delBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '客户价值评估信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },


        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai305',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWBY002') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBY002 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWBY001') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwby001',
                                            searchParam: {

                                            },
                                            columnList: [
                                                {label: '评估编号', prop: 'GWBY001', width: '150'},
                                                {label: '客户', prop: 'GWBY002_NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBY001 = data.GWBY001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择评估编号',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if (field == 'GWBYORIU') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'},
                                                {label: '账户', prop: 'ACCOUNT', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBYORIU = data.ID;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择创建人',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initHeadTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai300();

});